package com.powernode.teatraceability.controller.login;


import com.powernode.teatraceability.pojo.login.User;
import com.powernode.teatraceability.service.user.AuthorizeService;
import com.powernode.teatraceability.utils.JsonData;
import org.hibernate.validator.constraints.Length;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import javax.validation.constraints.Pattern;

/**
 * ClassName: AuthorizeController
 * Description:
 *
 * @Author: jdz
 * @Create: 2023/4/21 - 0:58
 */
@Validated
@RestController
@RequestMapping("/api/auth")
public class AuthorizeController {
    private final String EMAIL_REGEXP = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+.[a-zA-Z]{2,}$";
    private final String USERNAME_REGEXP = "^[a-zA-Z0-9_\\u4e00-\\u9fa5]+$";
    @Resource
    AuthorizeService authorizeService;
    @PostMapping("/valid-register-email")
    public JsonData<String> validateRegisterEmail(@Pattern(regexp =  EMAIL_REGEXP)
                                          @RequestParam("email") String email,
                                                  HttpSession session) {
        final String isValid = authorizeService.sendValidateEmail(email, session.getId(), false);
        if (isValid == null) {
            return JsonData.success("邮件已发送, 请注意查看");
        } else {
            return JsonData.failure(400, "false", isValid);
        }
    }
    @PostMapping("/valid-reset-email")
    public JsonData<String> validateResetEmail(@Pattern (regexp =  EMAIL_REGEXP)
                                          @RequestParam("email") String email,
                                          HttpSession session) {
        final String isValid = authorizeService.sendValidateEmail(email, session.getId(), true);
        if (isValid == null) {
            return JsonData.success("邮件已发送, 请注意查看");
        } else {
            return JsonData.failure(400, isValid);
        }
    }
    @PostMapping("/register")
    public JsonData<String> registerUser( @RequestBody User user,
                                            HttpSession session) {
        final String registerInfo = authorizeService.validateAndRegister(user,
                                                                         session.getId());
        if (registerInfo == null) {
            return JsonData.success("注册成功！");
        } else {
            return JsonData.failure(400, registerInfo);
        }
    }
    /**
     * 1. 发送邮件
     * 2. 验证验证码是否正确，正确就在Session中存一个标记
     * 3. 用户发起重置密码请求，如果存在标记，就重置成功
     */
    @PostMapping("start-reset")
    public JsonData<String> startReset(@Pattern (regexp = EMAIL_REGEXP)
                                           @RequestParam("email") String email,
                                       @Length(min = 6, max = 6)
                                           @RequestParam("code") String code,
                                       HttpSession session) {
        final String validateOnly = authorizeService.validateOnly(email, code, session.getId());
        if (validateOnly == null) {
            session.setAttribute("reset-password", email);
            return JsonData.success();
        } else {
            return JsonData.failure(400,validateOnly);
        }
    }
    @PostMapping("/do-reset")
    public JsonData<String> resetPassword(@Length(min = 1)
                                          @RequestParam("password")
                                              String password,
                                          HttpSession session) {
        final String email = (String) session.getAttribute("reset-password");
        if (email == null) {
            return JsonData.failure(401, "请先完成邮箱验证");
        } else if (authorizeService.resetPassword(password, email)){
            session.removeAttribute("reset-password");
            return JsonData.success("密码重置成功！");
        } else {
            return JsonData.failure(500,"内部错误请联系管理员！");
        }
    }
}
